<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>类型</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"c/language/types","wgTitle":"c/language/types","wgCurRevisionId":72801,"wgArticleId":1078,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"c/language/types","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-c {line-height: normal;}
.source-c li, .source-c pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for c
 * CSS class: source-c, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.c.source-c .de1, .c.source-c .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.c.source-c  {font-family:monospace;}
.c.source-c .imp {font-weight: bold; color: red;}
.c.source-c li, .c.source-c .li1 {font-weight: normal; vertical-align:top;}
.c.source-c .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.c.source-c .li2 {font-weight: bold; vertical-align:top;}
.c.source-c .kw1 {color: #0000dd;}
.c.source-c .kw2 {color: #0000ff;}
.c.source-c .kw3 {color: #0000dd;}
.c.source-c .kw4 {color: #0000ff;}
.c.source-c .co1 {color: #909090;}
.c.source-c .co2 {color: #339900;}
.c.source-c .coMULTI {color: #ff0000; font-style: italic;}
.c.source-c .es0 {color: #008000; font-weight: bold;}
.c.source-c .es1 {color: #008000; font-weight: bold;}
.c.source-c .es2 {color: #008000; font-weight: bold;}
.c.source-c .es3 {color: #008000; font-weight: bold;}
.c.source-c .es4 {color: #008000; font-weight: bold;}
.c.source-c .es5 {color: #008000; font-weight: bold;}
.c.source-c .br0 {color: #008000;}
.c.source-c .sy0 {color: #008000;}
.c.source-c .sy1 {color: #000080;}
.c.source-c .sy2 {color: #000040;}
.c.source-c .sy3 {color: #000040;}
.c.source-c .sy4 {color: #008080;}
.c.source-c .st0 {color: #008000;}
.c.source-c .nu0 {color: #000080;}
.c.source-c .nu6 {color:#000080;}
.c.source-c .nu8 {color:#000080;}
.c.source-c .nu12 {color:#000080;}
.c.source-c .nu16 {color:#000080;}
.c.source-c .nu17 {color:#000080;}
.c.source-c .nu18 {color:#000080;}
.c.source-c .nu19 {color:#000080;}
.c.source-c .ln-xtra, .c.source-c li.ln-xtra, .c.source-c div.ln-xtra {background-color: #ffc;}
.c.source-c span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-c_language_types skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">类型</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../c.html" title="c">c</a>‎ | <a href="../language.html" title="c/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../c.html" title="c"> C</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="c/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="c/header">头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../types.html" title="c/types">类型支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../program.html" title="c/program">程序工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../variadic.html" title="c/variadic">变参数函数支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../memory.html" title="c/memory">动态内存管理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="c/error">错误处理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../chrono.html" title="c/chrono">日期和时间工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="c/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="c/algorithm">算法</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="c/numeric">数值</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="c/io">输入/输出支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="c/locale">本地化支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="c/thread">线程支持</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="c/atomic">原子操作</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="c/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="c/language">C 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="basic_concepts.html" title="c/language/basic concepts">基本概念</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="../comment.html" title="c/comment">注释</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="ascii.html" title="c/language/ascii">ASCII</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="escape.html" title="c/language/escape">转义序列</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="translation_phases.html" title="c/language/translation phases">翻译阶段</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="punctuators.html" title="c/language/punctuators">标点</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="identifier.html" title="c/language/identifier">标识符</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="scope.html" title="c/language/scope">作用域</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="lifetime.html" title="c/language/lifetime">生存期</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="name_space.html" title="c/language/name space">查找与命名空间</a></td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink">类型</strong></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="arithmetic_types.html" title="c/language/arithmetic types">算术类型</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="object.html" title="c/language/object">对象与对齐</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="main_function.html" title="c/language/main function"><code>main()</code> 函数</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="https://zh.cppreference.com/mwiki/index.php?title=c/language/as_if&amp;action=edit&amp;redlink=1" class="new" title="c/language/as if（页面不存在）">如同规则</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="behavior.html" title="c/language/behavior">未定义行为</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="memory_model.html" title="c/language/memory model">内存模型与数据竞争</a></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p><small>（内建类型上的更多细节参阅<a href="arithmetic_types.html" title="c/language/arithmetic types">算术类型</a>和 C 库所提供的<a href="../types.html" title="c/types">类型相关的工具列表</a>）</small>
</p><p><a href="object.html" title="c/language/object">对象</a>、<a href="functions.html" title="c/language/functions">函数</a>和<a href="expressions.html" title="c/language/expressions">表达式</a>拥有称为<i>类型</i>的属性，它确定存储于对象或表达式求值所得的二进制值的转译方式。
</p>
<h3><span class="mw-headline" id=".E7.B1.BB.E5.9E.8B.E5.88.86.E7.B1.BB">类型分类</span></h3>
<p>C 类型系统由下列类型组成：
</p>
<ul><li>类型 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">void</span></span></span>
</li><li>基本类型
</li></ul>
<dl><dd><ul><li>类型 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">char</span></span></span>
</li><li>有符号整数类型
</li></ul>
<dl><dd><ul><li>标准： <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">signed</span> <span class="kw4">char</span></span></span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">short</span></span></span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">int</span></span></span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">long</span></span></span><span class="t-rev-inl t-since-c99"><span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">long</span> <span class="kw4">long</span></span></span></span> <span><span class="t-mark-rev t-since-c99">(C99 起)</span></span></span>
</li></ul>
</dd></dl>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c99"><td>
<dl><dd><dl><dd><ul><li>扩展：实现定义，例如 <span class="t-c"><span class="mw-geshi c source-c">__int128</span></span>
</li></ul>
</dd></dl>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-c99">(C99 起)</span></td></tr>
</table>
<dl><dd><ul><li>无符号整数类型
</li></ul>
<dl><dd><ul><li>标准：<span class="t-rev-inl t-since-c99"><span> <span class="t-c"><span class="mw-geshi c source-c">_Bool</span></span> 、</span> <span><span class="t-mark-rev t-since-c99">(C99 起)</span></span></span> <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">char</span></span></span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">short</span></span></span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">int</span></span></span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">long</span></span></span> 、<span class="t-rev-inl t-since-c99"><span><span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">long</span> <span class="kw4">long</span></span></span></span> <span><span class="t-mark-rev t-since-c99">(C99 起)</span></span></span>
</li></ul>
</dd></dl>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c99"><td>
<dl><dd><dl><dd><ul><li>扩展：实现定义，例如 <span class="t-c"><span class="mw-geshi c source-c">__uint128</span></span>
</li></ul>
</dd></dl>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-c99">(C99 起)</span></td></tr>
</table>
<dl><dd><ul><li>浮点类型
</li></ul>
<dl><dd><ul><li>实浮点类型： <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">float</span></span></span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">double</span></span></span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">long</span> <span class="kw4">double</span></span></span>
</li></ul>
</dd></dl>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c23"><td>
<dl><dd><dl><dd><ul><li>十进制实浮点类型： <span class="t-c"><span class="mw-geshi c source-c">_Decimal32</span></span> 、 <span class="t-c"><span class="mw-geshi c source-c">_Decimal64</span></span> 、 <span class="t-c"><span class="mw-geshi c source-c">_Decimal128</span></span>
</li></ul>
</dd></dl>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-c23">(C23 起)</span></td></tr>
<tr class="t-rev t-since-c99"><td>
<dl><dd><dl><dd><ul><li>复数类型： <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">float</span> _Complex</span></span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">double</span> _Complex</span></span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">long</span> <span class="kw4">double</span> _Complex</span></span>
</li><li>虚数类型： <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">float</span> _Imaginary</span></span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">double</span> _Imaginary</span></span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">long</span> <span class="kw4">double</span> _Imaginary</span></span>
</li></ul>
</dd></dl>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-c99">(C99 起)</span></td></tr>
</table>
<ul><li> <a href="enum.html" title="c/language/enum">枚举类型</a>
</li></ul>
<ul><li> 派生类型
</li></ul>
<dl><dd><ul><li> <a href="array.html" title="c/language/array">数组类型</a>
</li><li> <a href="struct.html" title="c/language/struct">结构体类型</a>
</li><li> <a href="union.html" title="c/language/union">联合体类型</a>
</li><li> <a href="functions.html" title="c/language/functions">函数类型</a>
</li><li> <a href="pointer.html" title="c/language/pointer">指针类型</a>
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c11"><td>
<dl><dd><ul><li> <a href="atomic.html" title="c/language/atomic">原子类型</a>
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-c11">(C11 起)</span></td></tr>
</table>
<p>对于上面列出的每个类型，可以存在数种其类型的限定版本，对应 <a href="const.html" title="c/language/const"><code>const</code></a> 、 <a href="volatile.html" title="c/language/volatile"><code>volatile</code></a> 和 <a href="restrict.html" title="c/language/restrict"><code>restrict</code></a> 限定符的一、二或全部三个组合（在限定符的语义所允许处）。
</p>
<h4><span class="mw-headline" id=".E7.B1.BB.E5.9E.8B.E7.BB.84.E5.88.AB">类型组别</span></h4>
<ul><li> <i>对象类型</i>：所有不是函数类型的类型
</li><li> <i>字符类型</i>： <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">char</span></span></span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">signed</span> <span class="kw4">char</span></span></span> 、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">char</span></span></span>
</li><li> <i>整数类型</i>： <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">char</span></span></span> 、有符号整数类型、无符号整数类型、枚举类型
</li><li> <i>实数类型</i>：整数类型和实浮点类型
</li><li> <a href="arithmetic_types.html" title="c/language/arithmetic types"><i>算术类型</i></a>：整数类型和浮点类型
</li><li> <i>标量类型</i>：算术类型和指针类型
</li><li> <i>聚合类型</i>：数组类型和结构体类型
</li><li> <i>派生声明器类型</i>：数组类型、函数类型和指针类型
</li></ul>
<h3><span class="mw-headline" id=".E5.85.BC.E5.AE.B9.E7.B1.BB.E5.9E.8B">兼容类型</span></h3>
<p>C 程序中，在<i>不同翻译单元</i>中涉及同一对象或函数的声明，不必拥有相同类型。它们只需要拥有相似的类型，正式而言即<i>兼容类型</i>。同样的规则应用到函数调用和左值访问；实参类型必须与形参类型<i>兼容</i>，而左值表达式类型必须与被访问对象的类型<i>兼容</i>。
</p><p>类型 <code>T</code> 与 <code>U</code> 兼容，若
</p>
<ul><li> 它们是同一类型（同名或由 <a href="typedef.html" title="c/language/typedef">typedef</a> 引入的别名）
</li><li> 它们是兼容的无限定类型的等同 cvr 限定版本
</li><li> 它们是指针类型，并指向兼容类型
</li><li> 它们是数组类型，且
</li></ul>
<dl><dd><ul><li> 其元素类型兼容，且
</li><li> 若都拥有常量大小，则大小相同。注意：未知边界数组与任何兼容元素类型的数组兼容。<span class="t-rev-inl t-since-c99"><span> VLA 与任何兼容元素类型的数组兼容。</span> <span><span class="t-mark-rev t-since-c99">(C99 起)</span></span></span>
</li></ul>
</dd></dl>
<ul><li> 它们都是结构体/联合体/枚举类型，且
</li></ul>
<dl><dd><ul><li><span class="t-mark-rev t-since-c99">(C99)</span>若一者以标签声明，则另一者必须以同一标签声明。
</li><li> 若它们都是完整类型，则其成员必须在数量上准确对应，以兼容类型声明，并拥有匹配的名称。
</li><li> 另外，若它们都是枚举，则对应成员亦必须拥有相同值。
</li><li> 另外，若它们是结构体或联合体，则
</li></ul>
<dl><dd><ul><li> 对应的元素必须以同一顺序声明（仅结构体）
</li><li> 对应的位域必须有相同宽度。
</li></ul>
</dd></dl>
</dd></dl>
<ul><li> 一者为枚举类型，而另一者为该枚举的底层类型
</li><li> 它们是函数类型，且
</li></ul>
<dl><dd><ul><li> 其返回类型兼容
</li><li> 它们都使用参数列表，参数数量（包括省略号的使用）相同，而其对应参数，在应用数组到指针和函数到指针类型调整，及剥除顶层限定符后，拥有相同类型
</li><li> 一个是旧式（无参数）定义，另一个有参数列表，参数列表不使用省略号，而每个参数（在函数参数类型调整后）都与默认参数提升后的对应旧式参数兼容
</li><li> 一个是旧式（无参数）声明，另一个拥有参数列表，参数列表不使用省略号，而所有参数（在函数参数类型调整后）不受默认参数提升影响
</li></ul>
</dd></dl>
<p>类型 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">char</span></span></span> 既不与 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">signed</span> <span class="kw4">char</span></span></span> 兼容，也不与 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">char</span></span></span> 兼容。
</p><p>若涉及同一对象或函数的二个声明不使用兼容类型，则程序的行为未定义。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co1">// 翻译单元 1</span>
<span class="kw1">struct</span> S <span class="br0">{</span><span class="kw4">int</span> a<span class="sy4">;</span><span class="br0">}</span><span class="sy4">;</span>
<span class="kw2">extern</span> <span class="kw1">struct</span> S <span class="sy2">*</span>x<span class="sy4">;</span>  <span class="co1">// 与 TU2 的 x 兼容，但不与 TU3 的 x 兼容</span>
<span class="co1">// 翻译单元 2</span>
<span class="kw1">struct</span> S<span class="sy4">;</span>
<span class="kw2">extern</span> <span class="kw1">struct</span> S <span class="sy2">*</span>x<span class="sy4">;</span> <span class="co1">// 与两个 x 兼容</span>
<span class="co1">// 翻译单元 3</span>
<span class="kw1">struct</span> S <span class="br0">{</span><span class="kw4">float</span> a<span class="sy4">;</span><span class="br0">}</span><span class="sy4">;</span>
<span class="kw2">extern</span> <span class="kw1">struct</span> S <span class="sy2">*</span>x<span class="sy4">;</span> <span class="co1">// 与 TU2 的 x 兼容，但不与 TU1 的 x 兼容</span>
 
<span class="co1">// 行为未定义</span></pre></div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co1">// 翻译单元 1</span>
<span class="co2">#include &lt;stdio.h&gt;</span>
<span class="kw1">struct</span> s <span class="br0">{</span><span class="kw4">int</span> i<span class="sy4">;</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 与 TU3 的 s 兼容，但不与 TU2 的 s 兼容</span>
<span class="kw2">extern</span> <span class="kw1">struct</span> s x <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">0</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 与 TU3 的 x 兼容</span>
<span class="kw2">extern</span> <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 与 TU2 的 f 兼容</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
   f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
   <span class="kw1">return</span> x.<span class="me1">i</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="co1">// 翻译单元 2</span>
<span class="kw1">struct</span> s <span class="br0">{</span><span class="kw4">float</span> f<span class="sy4">;</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 与 TU4 的 s 兼容，但不与 TU1 的 s 兼容</span>
<span class="kw2">extern</span> <span class="kw1">struct</span> s y <span class="sy1">=</span> <span class="br0">{</span><span class="nu16">3.14</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 与 TU4 的 y 兼容</span>
<span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span> <span class="co1">// 与 TU1 的 f 兼容</span>
<span class="br0">{</span>
   <span class="kw1">return</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="co1">// 翻译单元 3</span>
<span class="kw1">struct</span> s <span class="br0">{</span><span class="kw4">int</span> i<span class="sy4">;</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 与 TU1 的 s 兼容，但不与 TU2 的 s 兼容</span>
<span class="kw2">extern</span> <span class="kw1">struct</span> s x<span class="sy4">;</span> <span class="co1">// 与 TU1 的 x 兼容</span>
<span class="co1">// 翻译单元 4</span>
<span class="kw1">struct</span> s <span class="br0">{</span><span class="kw4">float</span> f<span class="sy4">;</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 与 TU2 的 s 兼容，但不与 TU1 的 s 兼容</span>
<span class="kw2">extern</span> <span class="kw1">struct</span> s y<span class="sy4">;</span> <span class="co1">// 与 TU2 的 y 兼容</span>
 
<span class="co1">// 行为良好定义：只有对象或函数的多个声明，而非类型自身必须拥有兼容类型</span></pre></div></div>
<p>注意： C++ 没有兼容类型的概念。在不同翻译单元中声明二个兼容但不等同的类型的 C 程序不是合法的 C++ 程序。
</p>
<h3><span class="mw-headline" id=".E5.90.88.E6.88.90.E7.B1.BB.E5.9E.8B">合成类型</span></h3>
<p>合成类型能从二个兼容的类型构造；它是与两个类型兼容，并满足下列条件的类型：
</p>
<ul><li>若两个类型均为数组类型，则应用下列规则：
</li></ul>
<dl><dd><ul><li>若一个类型是常量大小数组，则合成类型为该大小的数组。
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c99"><td>
<dl><dd><ul><li>否则，若一个类型是 VLA ，而尚未求值指定其大小的表达式，则行为未定义。
</li><li>否则，若一个类型为已指定大小的 VLA ，则合成类型为该大小的 VLA 。
</li><li>否则，若一个类型为未指定大小的 VLA ，则合成类型为未指定大小的 VLA 。
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-c99">(C99 起)</span></td></tr>
</table>
<dl><dd><ul><li>否则，两个数组类型都有未知大小，而合成类型为未知大小的数组。
</li></ul>
<dl><dd>合成类型的元素类型是二个元素类型的合成类型。
</dd></dl>
</dd></dl>
<ul><li>若一个类型是有参数类型列表（函数原型）的函数类型，则合成类型为有该参数类型列表的函数原型。
</li><li>若两个类型均为有参数类型列表的函数类型，则合成类型的参数类型列表中的每个参数类型，是对应参数的合成类型。
</li></ul>
<p>这些规则递归地应用到二个类型的派生来源类型。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co1">// 给定一下二个文件作用域声明：</span>
<span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">)</span><span class="br0">(</span><span class="br0">)</span>, <span class="kw4">double</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">)</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">)</span><span class="br0">(</span><span class="kw4">char</span> <span class="sy2">*</span><span class="br0">)</span>, <span class="kw4">double</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">)</span><span class="br0">[</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span>
<span class="co1">// 生成的函数合成类型为：</span>
<span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">)</span><span class="br0">(</span><span class="kw4">char</span> <span class="sy2">*</span><span class="br0">)</span>, <span class="kw4">double</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">)</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span></pre></div></div>
<p>对于拥有内部或外部<a href="storage_duration.html" title="c/language/storage duration">链接</a>，并在其先前声明已经可见的作用域中再次声明的标识符，若先前的声明指定了内部或外部链接，则在后一声明中的标识符类型成为合成类型。
</p>
<h3><span class="mw-headline" id=".E4.B8.8D.E5.AE.8C.E6.95.B4.E7.B1.BB.E5.9E.8B">不完整类型</span></h3>
<p>不完整类型是缺乏足以确定其对象大小的信息对象类型。不完整类型可以在翻译单元的某些点完整。
</p><p>下列类型不完整：
</p>
<ul><li> 类型 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">void</span></span></span> 。此类型不能完整。
</li><li> 大小未知的数组。之后指定大小的声明能使之完整。
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw2">extern</span> <span class="kw4">char</span> a<span class="br0">[</span><span class="br0">]</span><span class="sy4">;</span> <span class="co1">// a 的类型不完整（这通常出现于头文件）</span>
<span class="kw4">char</span> a<span class="br0">[</span><span class="nu0">10</span><span class="br0">]</span><span class="sy4">;</span>      <span class="co1">// a 的类型现在完整（这通常出现于源文件）</span></pre></div></div>
<ul><li> 内容未知的结构体或联合体类型。在同一作用域的后面，定义同一结构体或联合体的内容的声明能使之完整。
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw1">struct</span> node <span class="br0">{</span>
  <span class="kw1">struct</span> node <span class="sy2">*</span>next<span class="sy4">;</span> <span class="co1">// struct node 在此点不完整</span>
<span class="br0">}</span><span class="sy4">;</span> <span class="co1">// struct node 在此点完整</span></pre></div></div>
<h3><span class="mw-headline" id=".E7.B1.BB.E5.9E.8B.E5.90.8D">类型名</span></h3>
<p>可能在异于<a href="declarations.html" title="c/language/declarations">声明</a>的语境中必须指名类型。这些情况下使用<i>类型名</i>，即 <i>type-specifiers</i> 及 <i>type-qualifiers</i> 后随 <i>declarator</i> 的列表（见<a href="declarations.html" title="c/language/declarations">声明</a>），它在文法上与会用来声明此类型的单个对象或函数的列表准确相同，除了省略掉标识符：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> n<span class="sy4">;</span> <span class="co1">// 声明 int </span>
<span class="kw4">sizeof</span><span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 使用类型名</span>
 
<span class="kw4">int</span> <span class="sy2">*</span>a<span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="sy4">;</span> <span class="co1">// 声明 3 个指向 int 指针的数组</span>
<span class="kw4">sizeof</span><span class="br0">(</span><span class="kw4">int</span> <span class="sy2">*</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 使用类型名</span>
 
<span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span>p<span class="br0">)</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="sy4">;</span> <span class="co1">// 声明指向 3 个 int 的数组的指针</span>
<span class="kw4">sizeof</span><span class="br0">(</span><span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">)</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 使用类型名</span>
 
<span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span>a<span class="br0">)</span><span class="br0">[</span><span class="sy2">*</span><span class="br0">]</span> <span class="co1">// （在函数参数中）声明指向 VLA 的指针</span>
<span class="kw4">sizeof</span><span class="br0">(</span><span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">)</span><span class="br0">[</span><span class="sy2">*</span><span class="br0">]</span><span class="br0">)</span> <span class="co1">// （在函数参数中）使用类型名</span>
 
<span class="kw4">int</span> <span class="sy2">*</span>f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明函数</span>
<span class="kw4">sizeof</span><span class="br0">(</span><span class="kw4">int</span> <span class="sy2">*</span><span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 使用类型名</span>
 
<span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span>p<span class="br0">)</span><span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明指向函数指针</span>
<span class="kw4">sizeof</span><span class="br0">(</span><span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">)</span><span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 使用类型名</span>
 
<span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span><span class="kw4">const</span> a<span class="br0">[</span><span class="br0">]</span><span class="br0">)</span><span class="br0">(</span><span class="kw4">unsigned</span> <span class="kw4">int</span>, ...<span class="br0">)</span> <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">0</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 声明指向函数指针的数组</span>
<span class="kw4">sizeof</span><span class="br0">(</span><span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span><span class="kw4">const</span> <span class="br0">[</span><span class="br0">]</span><span class="br0">)</span><span class="br0">(</span><span class="kw4">unsigned</span> <span class="kw4">int</span>, ...<span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 使用类型名</span></pre></div></div>
<p>除了围绕标识符的冗余括号在类型名中有意义，并表示“不指定参数的函数”：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> <span class="br0">(</span>n<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明 int 类型的 n</span>
<span class="kw4">sizeof</span><span class="br0">(</span><span class="kw4">int</span> <span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 使用类型“返回 int 的函数”</span></pre></div></div>
<p>类型名用于下列场合：
</p>
<ul><li> <a href="cast.html" title="c/language/cast">转型运算符</a>
</li><li> <a href="sizeof.html" title="c/language/sizeof"><code>sizeof</code></a>
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c99"><td>
<ul><li> <a href="compound_literal.html" title="c/language/compound literal">复合字面量</a>
</li></ul>
</td>
<td><span class="t-mark-rev t-since-c99">(C99 起)</span></td></tr>
<tr class="t-rev t-since-c11"><td>
<ul><li> <a href="generic.html" title="c/language/generic">泛型选择</a>
</li><li> <a href="_Alignof.html" title="c/language/ Alignof"><code>_Alignof</code></a>
</li><li> <a href="_Alignas.html" title="c/language/ Alignas"><code>_Alignas</code></a>
</li><li> <a href="atomic.html" title="c/language/atomic"><code>_Atomic</code></a> （用作类型指定符时）
</li></ul>
</td>
<td><span class="t-mark-rev t-since-c11">(C11 起)</span></td></tr>
</table>
<p><br>
类型名可引入新类型：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">void</span><span class="sy2">*</span> p <span class="sy1">=</span> <span class="br0">(</span><span class="kw4">void</span><span class="sy2">*</span><span class="br0">)</span><span class="br0">(</span><span class="kw1">struct</span> X <span class="br0">{</span><span class="kw4">int</span> i<span class="sy4">;</span><span class="br0">}</span> <span class="sy2">*</span><span class="br0">)</span><span class="nu0">0</span><span class="sy4">;</span>
<span class="co1">// 用于转型表达式的 "struct X {int i;}*"</span>
<span class="co1">// 引入新类型 "struct X"</span>
<span class="kw1">struct</span> X x <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">1</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// struct X 现在在作用域中</span></pre></div></div>
<h3><span class="mw-headline" id=".E5.BC.95.E7.94.A8">引用</span></h3>
<div class="t-ref-std-17">
<ul><li> C17 标准（ISO/IEC 9899:2018）：
</li></ul>
<dl><dd><ul><li> 6.2.5 Types (p: 31-33)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.2.6 Representations of types (p: 31-35)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.2.7 Compatible type and composite type (p: 35-36)
</li></ul>
</dd></dl>
</div>
<div class="t-ref-std-11">
<ul><li> C11 标准（ISO/IEC 9899:2011）：
</li></ul>
<dl><dd><ul><li> 6.2.5 Types (p: 39-43)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.2.6 Representations of types (p: 44-46)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.2.7 Compatible type and composite type (p: 47-48)
</li></ul>
</dd></dl>
</div>
<div class="t-ref-std-c99">
<ul><li> C99 标准（ISO/IEC 9899:1999）：
</li></ul>
<dl><dd><ul><li> 6.2.5 Types (p: 33-37)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.2.6 Representations of types (p: 37-40)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.2.7 Compatible type and composite type (p: 40-41)
</li></ul>
</dd></dl>
</div>
<div class="t-ref-std-c89">
<ul><li> C89/C90 标准（ISO/IEC 9899:1990）： 
</li></ul>
<dl><dd><ul><li> 3.1.2.5 Types 
</li></ul>
</dd></dl>
<dl><dd><ul><li> 3.1.2.6 Compatible type and composite type 
</li></ul>
</dd></dl>
</div>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<table class="t-dsc-begin">

</table>

<!-- 
NewPP limit report
Preprocessor visited node count: 2708/1000000
Preprocessor generated node count: 5823/1000000
Post‐expand include size: 37375/2097152 bytes
Template argument size: 8959/2097152 bytes
Highest expansion depth: 13/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:1078-0!*!0!!zh!*!zh!* and timestamp 20211216194643 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=c/language/types&amp;oldid=72801">https://zh.cppreference.com/mwiki/index.php?title=c/language/types&amp;oldid=72801</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/c/language/type">Online version</a></li><li>Offline version retrieved 2022-01-01 00:37.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2021年10月17日 (星期日) 07:12。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过43,198次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 1.162 secs. -->
	</body>
<!-- Cached 20211216194643 -->
</html>